<?php
class hrd_g13_model extends MY_Model {
	var $nama_tabel = 'hrd_g13';

	function GetJSON($idPeriode) {
		$ls_sql	= "	SELECT	a.*, b.nama_karyawan, c.nama_periode_thr
					FROM	hrd_g13 a
					LEFT JOIN hrd_karyawan b ON a.id_karyawan_fk=b.id_karyawan_pk
					LEFT JOIN hrd_periode_thr c ON a.id_periode_thr_fk=c.id_periode_thr_pk
					WHERE id_periode_thr_fk = '".$idPeriode."'
					ORDER BY ".(post('sort')=='' ? 'b.nama_karyawan' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		$list = $this->db->query($ls_sql)->result_array();
		$data = array('total' => count($list), 'rows' => $list);

		return json_encode( $data );
		// print_r($ls_sql);
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_g13_pk' => $arr_rule['id_g13_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	// Untuk detail G13
	function GetDetailJSON($idG13) {
		$ls_sql	= "	SELECT	a.*, b.nama_kode_bayar, b.id_grub_kode_bayar_fk
					FROM	hrd_detail_g13 a
					LEFT JOIN hrd_kode_bayar b ON a.id_kode_bayar_fk = b.id_kode_bayar_pk
					where id_g13_fk = '".$idG13."'
					ORDER BY ".(post('sort')=='' ? 'id_g13_fk' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		$list = $this->db->query($ls_sql)->result_array();
		// Memisahkan pendapatan dan potongan
		$jmlPendapatan=0;
		$jmlPotongan=0;
		$jmlKoperasi=0;
		foreach ($list as $key => $value) {
			// ambil status parameter dari grub kode bayar
			$this->db->where('id_grub_bayar_pk', $value['id_grub_kode_bayar_fk']);
			$gb = $this->db->get('hrd_grub_kode_bayar')->row_array();

			if ($gb['status_parameter'] == 1) {
				$jmlPendapatan = $jmlPendapatan + $value['nilai'];
				$dg[] = array('nama_kode_bayar' => $value['nama_kode_bayar'], 
							  'potongan' => 0,
							  'pendapatan' => $value['nilai'],
							  'kontribusi_koperasi' => 0
							  );
			} elseif ($gb['status_parameter'] == 2) {
				$jmlPotongan = $jmlPotongan + $value['nilai'];
				$dg[] = array('nama_kode_bayar' => $value['nama_kode_bayar'], 
							  'potongan' => $value['nilai'],
							  'pendapatan' => 0,
							  'kontribusi_koperasi' => 0
							  );
			} elseif ($gb['status_parameter'] == 3) {
				$jmlKoperasi = $jmlKoperasi + $value['nilai'];
				$dg[] = array('nama_kode_bayar' => $value['nama_kode_bayar'], 
							  'potongan' => 0,
							  'pendapatan' => 0,
							  'kontribusi_koperasi' => $value['nilai']
							  );
			}
		}

		$footer[] = array('nama_kode_bayar' => '<b>Total</b>', 'pendapatan' => $jmlPendapatan, 'potongan' => $jmlPotongan, 'kontribusi_koperasi' => $jmlKoperasi);
		$terima = $jmlPendapatan - $jmlPotongan;
		$TotalPengeluaranKop = $jmlPendapatan + $jmlKoperasi;
		$footer[] = array('nama_kode_bayar' => '<b>Total Pengeluaran Koperasi</b> (A+C)', 'pendapatan' => $TotalPengeluaranKop, 'potongan' => 0, 'kontribusi_koperasi' => 0);
		$footer[] = array('nama_kode_bayar' => '<b>Yang Diterima</b> (A-B)', 'pendapatan' => $terima, 'potongan' => 0, 'kontribusi_koperasi' => 0);

		$data = array('total' => count($dg), 'rows' => $dg, 'footer' => $footer);
		return json_encode( $data );
	}
}